﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellInetAddress_h
#define CellInetAddress_h

#include "CellPrerequisites.h"

#if CELL_PLATFORM == CELL_PLATFORM_WIN32
	#include <WS2tcpip.h>
#elif CELL_PLATFORM == CELL_PLATFORM_LINUX
	#include <sys/socket.h>
	#include <netinet/in.h>
#else
	// TODO
#endif

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

/** 此类表示互联网协议 (IP) 地址。
@author
	Jiangwei Xu
*/
class _CellExport InetAddress
{
public:
	/** 默认构造函数。
	*/
	InetAddress(void);

	/** 拷贝构造函数。
	*/
	InetAddress(const InetAddress& other);

	/** 构造函数。
	*/
	explicit
	InetAddress(const String& addr);

	/** 构造函数。
	*/
	explicit
	InetAddress(uint port);

	/** 构造函数。
	*/
	InetAddress(const String& addr, uint port);

	/** 析构函数。
	*/
	virtual ~InetAddress(void);

public:
	/** 该地址是否是有效地址。
	*/
	virtual bool isValid(void) const = 0;

	/** 该地址是否符合 IPv4 规范。
	*/
	virtual bool isIPv4(void) const = 0;

	/** 该地址是否符合 IPv6 规范。
	*/
	virtual bool isIPv6(void) const = 0;

public:
	/** 赋值操作符。
	*/
	InetAddress& operator=(const InetAddress& r);

	/** 返回 IP 地址字符串（以文本表现形式）。
	*/
	const String& getHostAddress(void) const;

	/** 重新设置主机名。
	*/
	void setHostAddress(const String& addr);

	/** 返回主机服务号。
	*/
	uint getPort(void) const;

	/** 重新设置主机服务号。
	*/
	void setPort(uint port);

protected:
	String mHostAddress;
	uint mPort;
};

/** @} */
/** @} */
} // end namespace cell

#endif // CellInetAddress_h
